package cn.yt.hospital.controller;

import cn.yt.hospital.entity.Userses;
import cn.yt.hospital.service.IUsersesService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangtao
 * @since 2023-03-01
 */
@Controller
@RequestMapping("/hospital/userses")
public class UsersesController {
    @Autowired
    private IUsersesService iUsersesService;
    @RequestMapping("/login")
    public String login(){
        return "login";
    }


    /**
     * 登录验证的方法
     */
    @RequestMapping("doLogin")
    public String doLogin(@RequestParam("userid") Integer userid, @RequestParam("upass") String upass,
                          HttpServletRequest request, HttpServletResponse response){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userid",userid);
        queryWrapper.eq("upass",upass);
        Userses userses = iUsersesService.getOne(queryWrapper);
        if (userses!=null){
            System.out.println("不为空"+userses);
            //创建cookie
            Cookie cookie = new Cookie("userid",userid+"");
            //失效时间
            cookie.setMaxAge(60);
            cookie.setPath("/");
            //响应cookie到客户端
            response.addCookie(cookie);
            //登录成功
            return "redirect:/hospital/doctor/list";
        }else {
            //登录失败
            return "login";
        }
    }
}
